package probe.gui;

import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;


import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.SpringLayout;

import javax.swing.JScrollPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyledDocument;

import probe.Gui;
import javax.swing.border.BevelBorder;




public class MainWindowController implements KeyListener, MouseListener {

	
	private static String USER_COMMAND_OUTPUT_TXT="Command: ";
	private static String AWAITING_INPUT_TXT="Awaiting Input...";
	
	private static int VAR_DEFAULT_HEIGHT=600;
	private static int VAR_DEFAULT_WIDTH=400;
	private static int VAR_DEFAULT_START_X=Toolkit.getDefaultToolkit().getScreenSize().width/2-VAR_DEFAULT_WIDTH/2;
	private static int VAR_DEFAULT_START_Y=Toolkit.getDefaultToolkit().getScreenSize().height/2-VAR_DEFAULT_HEIGHT/2;
	
	private static int VAR_INPUTBOX_HEIGHT=100;
	private boolean _inputStarted;
	
	private JFrame _frame;
	private JTextPane _outputTextPane;
	private JTextPane _inputTextPane;
	private JScrollPane _outputScrollPane;
	private JScrollPane _InputScrollPane;
	private String _inputString;
	
	private StyledDocument _outputStyleDocument;
	private StyledDocument _inputStyleDocument;
	
	
//	private DefaultStyledDocument doc=new DefaultStyledDocument();
	
	private Gui _gui;
	
	public	MainWindowController(Gui gui) {
		
		_gui=gui;
	}
	
	

	/**
	 * Initialize the contents of the frame.
	 * @wbp.parser.entryPoint
	 */
	
	public void init() {
		
		_inputStarted=false;

		_frame = new JFrame();
		_frame.setTitle("Probe");
		_frame.setBounds(VAR_DEFAULT_START_X, VAR_DEFAULT_START_Y, VAR_DEFAULT_WIDTH, VAR_DEFAULT_HEIGHT);
		_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		_frame.setVisible(true);
		SpringLayout springLayout = new SpringLayout();
		_frame.getContentPane().setLayout(springLayout);
		
		
		_outputScrollPane = new JScrollPane(_outputTextPane);
		springLayout.putConstraint(SpringLayout.NORTH, _outputScrollPane, 10, SpringLayout.NORTH, _frame.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, _outputScrollPane, 10, SpringLayout.WEST, _frame.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, _outputScrollPane, -10, SpringLayout.EAST, _frame.getContentPane());
		_frame.getContentPane().add(_outputScrollPane);
		
		_InputScrollPane = new JScrollPane(_inputTextPane);
		_InputScrollPane.setViewportBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		springLayout.putConstraint(SpringLayout.NORTH, _InputScrollPane, -VAR_INPUTBOX_HEIGHT, SpringLayout.SOUTH, _frame.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, _InputScrollPane, -10, SpringLayout.SOUTH, _frame.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, _outputScrollPane, -10, SpringLayout.NORTH, _InputScrollPane);
		springLayout.putConstraint(SpringLayout.WEST, _InputScrollPane, 10, SpringLayout.WEST, _frame.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, _InputScrollPane, -10, SpringLayout.EAST, _frame.getContentPane());
		_frame.getContentPane().add(_InputScrollPane);

		_outputTextPane = new JTextPane();
		_outputTextPane.setEditable(false);
		_outputScrollPane.setViewportView(_outputTextPane);
		
		_inputTextPane = new JTextPane();
		_InputScrollPane.setViewportView(_inputTextPane);
		_inputTextPane.addKeyListener(this);
		_inputTextPane.requestFocusInWindow();
		_inputTextPane.setText(AWAITING_INPUT_TXT);		
		
		
		_outputStyleDocument=_outputTextPane.getStyledDocument();
		_inputStyleDocument=_inputTextPane.getStyledDocument();
		_inputStyleDocument.setLogicalStyle(0, Gui.NORMAL);
		
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		int key = arg0.getKeyCode();
		if(!_inputStarted&&_inputTextPane.getText().compareTo(AWAITING_INPUT_TXT)==0&&_inputTextPane.hasFocus())
		{	
			_inputTextPane.setText(null);
		}
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
	
		int key = arg0.getKeyCode();
		
		
		if(key==KeyEvent.VK_ENTER)
		{
			printUserInputToOutput(probe.Gui.NORMAL,"%s %s\n",USER_COMMAND_OUTPUT_TXT,_inputTextPane.getText());
			

			_inputString=_inputTextPane.getText();		
			try {
				_gui._root._breakdown.processCommand(_inputString);
				

				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//reset input textbox
			_inputTextPane.setText(AWAITING_INPUT_TXT);
			_inputTextPane.requestFocusInWindow();
			
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		int key = arg0.getKeyCode();
	}
	//use GUI static styles
	//if no args assume normal string
	public void printUserInputToOutput(Style style,String printfIdentifier, Object ... args)
	{
		String temp;
		//if no args assume normal string
		
		if(args.length>=1)
		{
			temp=String.format(printfIdentifier,  args);
		}
		else
		{
			temp=printfIdentifier;
		}
		

		try {
			_outputStyleDocument.setLogicalStyle(_outputStyleDocument.getLength(), style);
			_outputStyleDocument.insertString(_outputStyleDocument.getLength(),temp,null);
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
		_frame.revalidate();
		_InputScrollPane.revalidate();
		_inputTextPane.revalidate();
		
	}



	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
